package com.imooc.collection;



import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;


/**
 * Created by JiaJia on 2019/5/31 0031.
 */
public class MapTest {

    public Map<String,Student> students;

    public MapTest(){
        this.students=new HashMap<String, Student>();
    }
    public void testPut(){
        Scanner console=new Scanner(System.in);
        int i=0;
        while (i<3){
            System.out.println("请输入学生ID");
            String ID=console.next();
            Student st=students.get(ID);
            if (st==null){
                String name=console.next();
                Student newStudent=new Student(ID,name);
                students.put(ID,newStudent);
                System.out.println("成功添加学生"+students.get(ID).name);
                i++;
            }else {
                System.out.println("该学生ID已被占用");
                continue;
            }
        }
    }

    public void testKeySet(){
        Set<String> keySet=students.keySet();
        for (String stuId: keySet) {
            Student st=students.get(stuId);
            if (st!=null){
                System.out.println("学生："+st.name);
            }
        }
    }


    public static void main(String[] args) {
        MapTest mp=new MapTest();
        mp.testPut();
        mp.testKeySet();
    }
}